package com.sicilin.system.login.action;

import com.sicilin.common.ajax.vo.AjaxResponse;
import com.sicilin.common.exception.BusinessException;
import com.sicilin.system.login.service.LoginService;
import com.sicilin.system.login.vo.LoginInfo;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class LoginAction {

    @Autowired
    private LoginService loginService;

    @RequestMapping( value = "/login.json",method = RequestMethod.POST)
    @ResponseBody
    public AjaxResponse login(@RequestBody LoginInfo info) throws Exception{
        if(StringUtils.isBlank(info.getAccount()) || StringUtils.isBlank(info.getPassword())){throw new BusinessException("用户名和密码不能为空！");}
        try {
            loginService.login(info.getAccount(), info.getPassword());
        }catch (AuthenticationException ae) {
            throw new BusinessException("用户名或者密码错误！");
        }
        return new AjaxResponse("pages/index.jhtml");
    }

    @RequestMapping("/logout.json")
    public ModelAndView logout() throws Exception{
        loginService.logout();
        return new ModelAndView("login");
    }

}
